package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 护理项目分页查询
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingProjectVo> pageQuery(NursingProjectPageQueryDto dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        //条件查询
        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(dto);
        //通过PageBean的of方法快速构建一个对象
        return PageBean.of(page, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     * @param dto
     * @return
     */
    @Override
    public void add(NursingProjectDto dto) {
        NursingProject nurse = BeanUtil.toBean(dto, NursingProject.class);

//        nurse.setUpdateBy(1671403256519078138L);
//        nurse.setCreateBy(1671403256519078138L);

        nursingProjectMapper.insert(nurse);
    }

    /**
     * 根据Id查询回显
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return BeanUtil.toBean(nursingProject,NursingProjectVo.class);
    }

    /**
     * 修改护理项目
     * @param dto
     * @return
     */
    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        //1.将dto转化为dto
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);

        //2.补全属性信息
        nursingProject.setUpdateBy(1671403256519078138L);

        //3.调用Mapper操作数据库
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用或禁用
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        //mapper方法:主要是crud,不涉及任何业务逻辑,select get list query add save insert update delete remove
        NursingProject build = NursingProject.builder().id(id).status(status).updateTime(LocalDateTime.now()).updateBy(1671403256519078138L).build();
        nursingProjectMapper.update(build);
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //判断护理项目的状态
        NursingProject nurse = nursingProjectMapper.getById(id);

        if(nurse == null){
            //抛异常
            throw new RuntimeException("数据状态异常!");
        }

        if(nurse.getStatus()==1){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingProjectMapper.deleteById(id);

    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {

        return nursingProjectMapper.selectAll();
    }
}
